#!/usr/bin/env python3
# _*_coding:utf-8 _*_
__author__ = "Gao"
'''后台管理相关form验证'''

from django import forms
from django.forms import fields
from repository import models
from django.forms import ValidationError
from django.forms import widgets


class BaseInfoForm(forms.Form):
    '''验证base_info'''

    def __init__(self, request, *args, **kwargs):
        super(BaseInfoForm, self).__init__(*args, **kwargs)
        self.request = request

    nickname = fields.CharField(error_messages={'required': '请输入昵称'})
    blog_url = fields.CharField(error_messages={'required': '请输入博客地址'})
    blog_theme = fields.CharField()
    blog_title = fields.CharField(error_messages={'required': '请输入博客标题'})
    avatar = fields.CharField(required=False)
    
    def clean_blog_url(self):
        '''验证博客地址是否合法'''
        res = models.Blog.objects.filter(site=self.cleaned_data['blog_url']).exclude(user_id=self.request.session.get('nid')).exists()
        if res:
            raise ValidationError('该地址已被注册')
        return self.cleaned_data['blog_url']



class CategoryForm(forms.Form):
    '''验证文章分类'''

    def __init__(self,category_nid, blog_nid, *args, **kwargs):
        super(CategoryForm, self).__init__(*args, **kwargs)
        self.category_nid = category_nid
        self.blog_nid = blog_nid

    title = fields.CharField(error_messages={'required': '请输入分类名称'})

    def clean_title(self):
        if not self.category_nid:
            # 验证新增分类
            res = models.Category.objects.filter(blog_id=self.blog_nid, title=self.cleaned_data['title']).exists()
        else:
            # 验证编辑分类
            res = models.Category.objects.filter(blog_id=self.blog_nid, title=self.cleaned_data['title'])\
                .exclude(nid=self.category_nid).exists()
        if res:
            raise ValidationError('已创建相同分类')
        return self.cleaned_data['title']


class TagForm(forms.Form):
    '''验证文章标签'''

    def __init__(self,tag_nid, blog_nid, *args, **kwargs):
        super(TagForm, self).__init__(*args, **kwargs)
        self.tag_nid = tag_nid
        self.blog_nid = blog_nid

    title = fields.CharField(error_messages={'required': '请输入标签名称'})

    def clean_title(self):
        if not self.tag_nid:
            # 验证新增标签
            res = models.Tag.objects.filter(blog_id=self.blog_nid, title=self.cleaned_data['title']).exists()
        else:
            # 验证编辑标签
            res = models.Tag.objects.filter(blog_id=self.blog_nid, title=self.cleaned_data['title'])\
                .exclude(nid=self.tag_nid).exists()
        if res:
            raise ValidationError('已创建相同标签')
        return self.cleaned_data['title']
    
    
class EditArticleForm(forms.Form):
    '''编辑文章'''

    def __init__(self, blog_id, *args, **kwargs):
        super(EditArticleForm, self).__init__(*args, **kwargs)
        self.blog_id = blog_id
        self.fields['category'].choices = models.Category.objects.filter(blog_id=blog_id).values_list('nid', 'title')
        self.fields['tag'].choices = models.Tag.objects.filter(blog_id=blog_id).values_list('nid', 'title')


    title = fields.CharField(
        widget=widgets.TextInput(attrs={'class': 'form-control','id':'title','placeholder':'文章标题'})
    )
    summary = fields.CharField(
        widget=widgets.Textarea(
            attrs={'class': 'form-control', 'id': 'summary', 'placeholder': '文章简介','cols': '0', 'rows': '0'}
        )
    )
    content = fields.CharField(
        required=False,
        widget=widgets.Textarea(
            attrs={'name':'content','style':'width: 100%;min-height:500px;visibility:hidden;','cols': '0', 'rows': '0'}
        )
    )
    article_type = fields.ChoiceField(widget=widgets.RadioSelect, choices=models.Article.type_choices)
    category = fields.ChoiceField(widget=widgets.RadioSelect, choices=[])
    tag = fields.MultipleChoiceField(widget=widgets.CheckboxSelectMultiple, choices=[])
    
    

